//
//  BWYShoppingCarTableViewCell.m
//  BWYShoppingCar
//
//  Created by apple on 16/1/21.
//  Copyright © 2016年 毕炜勇. All rights reserved.
//

#import "BWYShoppingCarTableViewCell.h"

@implementation BWYShoppingCarTableViewCell

- (void)awakeFromNib {
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        self.backgroundColor = XNColor(251, 251, 251, 1);
        self.backgroundColor = [UIColor whiteColor];
        [self creatCellsView];
    }
    return self;
}

- (void)creatCellsView
{
    _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectedButton setImage:[UIImage imageNamed:@"对号正常.png"] forState:UIControlStateNormal];
    [_selectedButton setImage:[UIImage imageNamed:@"对号选中.png"] forState:UIControlStateSelected];
    _selectedButton.frame = CGRectMake(10, 35, 30, 30);
    _selectedButton.tag = 101;
    [_selectedButton addTarget:self action:@selector(isselectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_selectedButton];
    
    _GoodsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_selectedButton.frame.origin.x + 30, 10, 72, 72)];
    [self.contentView addSubview:_GoodsImageView];
    
    _goodsNameLable = [[UILabel alloc]initWithFrame:CGRectMake(_GoodsImageView.frame.origin.x + 80, 0, [UIScreen mainScreen].bounds.size.width - 50 - _GoodsImageView.frame.origin.x - 70 - 10, 35)];
    _goodsNameLable.textAlignment = NSTextAlignmentLeft;
    _goodsNameLable.font = [UIFont systemFontOfSize:14];
    _goodsNameLable.textColor = [UIColor blackColor];
    _goodsNameLable.numberOfLines = 0;
    [self.contentView addSubview:_goodsNameLable];
    
    _goodsPriceLable = [[UILabel alloc]initWithFrame:CGRectMake(_goodsNameLable.frame.origin.x, 55,[UIScreen mainScreen].bounds.size.width - 100 - _GoodsImageView.frame.origin.x - 70 - 10, 30)];
    _goodsPriceLable.textAlignment = NSTextAlignmentLeft;
    _goodsPriceLable.textColor = XNColor(236, 152, 18, 1);
    _goodsPriceLable.font = [UIFont systemFontOfSize:14];
    _goodsPriceLable.numberOfLines = 0;
    [self.contentView addSubview:_goodsPriceLable];
    
    _deletButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _deletButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 30, 10, 16, 18);
//    [_deletButton setTitle:@"" forState:UIControlStateNormal];
//    [_deletButton setTitle:@"" forState:UIControlStateHighlighted];
     [_deletButton setBackgroundImage:[UIImage imageNamed:@"删除"]forState:UIControlStateNormal];
    [_deletButton setBackgroundImage:[UIImage imageNamed:@"删除"]forState:UIControlStateDisabled];
    
    _deletButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_deletButton addTarget:self action:@selector(deletButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _deletButton.tag = 102;
    [self.contentView addSubview:_deletButton];
    
    _subtractButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [_subtractButton setTitle:@"减" forState:UIControlStateNormal];
//    [_subtractButton setTitle:@"减" forState:UIControlStateHighlighted];
    [_subtractButton setBackgroundImage:[UIImage imageNamed:@"可选-"]forState:UIControlStateNormal];
    [_subtractButton setBackgroundImage:[UIImage imageNamed:@"不可选-"]
                              forState:UIControlStateDisabled];
    _subtractButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 60, 23, 23);
    _subtractButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _subtractButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_subtractButton addTarget:self action:@selector(subStractButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _subtractButton.tag = 105;
    [self.contentView addSubview:_subtractButton];
    
    _goodsCountTextfield = [[UITextField alloc]initWithFrame:CGRectMake(_subtractButton.frame.origin.x + 27, _subtractButton.frame.origin.y, 36, 20)];
    _goodsCountTextfield.borderStyle = UITextBorderStyleNone;
    _goodsCountTextfield.textColor = [UIColor blackColor];
    _goodsCountTextfield.textAlignment = NSTextAlignmentCenter;
    _goodsCountTextfield.font = [UIFont systemFontOfSize:14];
    _goodsCountTextfield.enabled = NO;
    [self.contentView addSubview:_goodsCountTextfield];
    
    _addButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _addButton.frame = CGRectMake(_goodsCountTextfield.frame.origin.x + 36 + 2, _goodsCountTextfield.frame.origin.y, 23, 23);
    
    [_addButton setBackgroundImage:[UIImage imageNamed:@"可选+"]
                          forState:UIControlStateNormal];
    [_addButton setBackgroundImage:[UIImage imageNamed:@"不可选+"]
                          forState:UIControlStateDisabled];
    
//    [_addButton setTitle:@"加" forState:UIControlStateNormal];
//    [_addButton setTitle:@"加" forState:UIControlStateSelected];
//    [_addButton setTitle:@"加" forState:UIControlStateHighlighted];
    _addButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_addButton addTarget:self action:@selector(addButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _addButton.tag = 106;
    [self.contentView addSubview:_addButton];
}

- (void)customDataWithModel:(BWYShoppingCarModel *)model
{
    _model = model;
    NSString *str = [ReleaseORDebug stringByAppendingString:model.ImgUrl];
    NSURL *url = [NSURL URLWithString:str];
    [_GoodsImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"未上传图片"]];
    _goodsNameLable.text = [NSString stringWithFormat:@"%@",model.Name];
    _goodsNameLable.text = model.Name;
    _goodsPriceLable.text = [NSString stringWithFormat:@"%@元",model.Price];
    if (model.Count < 1) {
        _goodsCountTextfield.text = @"1";
    }else{
    _goodsCountTextfield.text = [NSString stringWithFormat:@"%ld",(long)model.Count];
    }
    if (model.isSelected == YES) {
        _selectedButton.selected = YES;
        [_selectedButton setImage:[UIImage imageNamed:@"对号选中"] forState:UIControlStateSelected];
    }else
    {
        _selectedButton.selected = NO;
        [_selectedButton setImage:[UIImage imageNamed:@"对号正常.png"] forState:UIControlStateNormal];
    }
}

- (void)isselectButtonClick:(UIButton *)button
{
    
    if (button.selected == YES) {
      [_selectedButton setImage:[UIImage imageNamed:@"对号正常.png"] forState:UIControlStateNormal];
        
    }else
    {
        [_selectedButton setImage:[UIImage imageNamed:@"对号选中.png"] forState:UIControlStateSelected];
    }
    button.selected = !button.selected;
    _model.isSelected = !_model.isSelected;
    [self.delegate isSelect:self andModel:_model];
}

- (void)deletButtonAction:(UIButton *)button
{
    [self.delegate isDelet:self andDeletButtonTag:button.tag];
}

- (void)subStractButtonAction:(UIButton *)button
{
//    if (_model.isSelected) {
        [self.delegate goodsCount:self andButtonTag:button.tag];
//    }
}

- (void)addButtonAction:(UIButton *)button
{
//    if (_model.isSelected) {
        [self.delegate goodsCount:self andButtonTag:button.tag];
//    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
